package com.travel.travelServer.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.Role;
import com.travel.travelServer.entity.vo.RoleVO;
import com.travel.travelServer.service.RoleService;
import com.travel.travelServer.utils.BaseController;
import com.travel.travelServer.utils.ResponseData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * (Role)表控制层
 *
 * @author makejava
 * @since 2023-10-24 16:46:28
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    @GetMapping
    public ResponseData selectAll(
            @RequestParam Map<String, Object> conditions
    ) {
        Page<RoleVO> page = new Page<>(Integer.valueOf(conditions.get("page").toString()),Integer.valueOf(conditions.get("size").toString()));
        return ResponseData.success(roleService.pageByRole(page, conditions));
    }

    @GetMapping("/{id}")
    public ResponseData selectOne(@PathVariable("id") Long id) {
        return ResponseData.success(roleService.getById(id));
    }

    @PostMapping
    public ResponseData insert(@RequestBody Role role) {
        return ResponseData.success(roleService.save(role));
    }

    @PutMapping
    public ResponseData update(@RequestBody Role role) {
        return ResponseData.success(roleService.updateById(role));
    }

    @DeleteMapping("/{id}")
    public ResponseData deleteById(@PathVariable("id") Long id) {
        return ResponseData.success(roleService.removeById(id));
    }

    @PostMapping("/batchDeleteRole")
    public ResponseData delete(@RequestBody Long[] idList) {
        return ResponseData.success(roleService.removeByIds(Arrays.asList(idList)));
    }

    @PostMapping("/setPermissions/{id}")
    public ResponseData setPermissions(@RequestBody Long[] idList, @PathVariable("id") Long id) {
        return ResponseData.success(roleService.setPermissions(Arrays.asList(idList), id));
    }
}

